package com.ruide.action;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruide.dao.BTReportDAO;
import com.ruide.dao.bankUsersDAO;
import com.ruide.vo.BTReport;
import com.ruide.vo.bankUsers;

/**
 * Servlet implementation class doDispoitAction
 */
public class doDispoitAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public doDispoitAction() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		bankUsersDAO dao=new bankUsersDAO();
		Integer userid=new Integer((String)request.getSession().getAttribute("userid"));
		Integer money=new Integer(request.getParameter("money"));
		bankUsers b=dao.findById(userid);
		dao.merge(money+b.getMoney(),userid);
		response.sendRedirect("main.jsp");
		
		Date date=new Date();
		Date da=null;
		SimpleDateFormat sdf=new SimpleDateFormat();
		try {
			da=sdf.parse(sdf.format(date));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		java.sql.Date d=new java.sql.Date(da.getTime());
		
		BTReport btr=new BTReport();
		btr.setUserid(userid);
		btr.setUseridto(userid);
		btr.setMoney(money);
		btr.setTime(d);
		BTReportDAO Bdao=new BTReportDAO();
		Bdao.save(btr);
	}

}
